package com.program.beautymakeup.utils;

import cn.hutool.core.lang.UUID;
import com.aliyun.oss.*;
import com.aliyun.oss.model.ObjectMetadata;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author bytedance
 * @ClassName AliOssUtils.java
 * @Description TODO
 * @createTime 2023年02月14日 21:14:00
 */
@Component
public class AliOssUtils {

    @Autowired
    private OSS oss;
    public static String BUCKET_NAME = "cd-meizhuang";
    public static String END_POINT = "oss-cn-chengdu.aliyuncs.com";

    public  String uploadFileAvatar(MultipartFile file) {
        String url = null;
        //获取上传文件输入流
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        //获取文件名称
        String fileName = file.getOriginalFilename();

        //保证文件名唯一，去掉uuid中的'-'
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        fileName = uuid + fileName;

        //把文件按日期分类，构建日期路径：avatar/2019/02/26/文件名
        //String datePath = new Date().toString("yyyy/MM/dd");
        String datePath = "2023";
        //拼接
        fileName = datePath + "/" + fileName;

        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentType("image/jpg");
        objectMetadata.setContentDisposition("inline");

        //调用oss方法上传到阿里云
        //第一个参数：Bucket名称
        //第二个参数：上传到oss文件路径和文件名称
        //第三个参数：上传文件输入流
        oss.putObject(BUCKET_NAME, fileName, inputStream, objectMetadata);
        //把上传后把文件url返回
        //https://xppll.oss-cn-beijing.aliyuncs.com/01.jpg
        url = "https://" + BUCKET_NAME + "." + END_POINT + "/" + fileName;
        return url;
    }


    public void deleteFile(String fileName) {
        // Endpoint以华东1（杭州）为例，其它Region请按实际情况填写。
        String endpoint = END_POINT;
        // 填写Bucket名称，例如examplebucket。
        String bucketName = BUCKET_NAME;
        // 填写Object完整路径，例如exampledir/exampleobject.txt。Object完整路径中不能包含Bucket名称。
        String objectName = fileName.replaceAll("https://" + bucketName + "." + endpoint + "/", "");

        try {
            // 删除文件。
            oss.deleteObject(bucketName, objectName);
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        }
    }


}
